1009E - Intercity Travelling - CodeForces Solution


combinatorics math probabilities *2000

Please click on ads to support us..

Python Code:

import decimal
import heapq
import math
import os
import random
import sys
from collections import Counter, deque, defaultdict
from io import BytesIO, IOBase
import bisect
from types import GeneratorType

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda: self.buffer.read().decode('ascii')
        self.readline = lambda: self.buffer.readline().decode('ascii')


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')


def isPrime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i * i <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i = i + 6
    return True


def lcm(a, b): return (a * b) // math.gcd(a, b)


def ciel(a, b):
    x = a // b
    if a % b == 0:
        return x
    return x + 1


def ints_get(): return map(int, input().strip().split())


def list_get(): return list(map(int, sys.stdin.readline().strip().split()))


def chars_get(): return list(map(str, sys.stdin.readline().strip().split()))


def ipn(): return int(input())


def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to

    return wrappedfunc




def main():
    n = ipn()
    a = list_get()
    mo = 998244353
    d = [a[0] * pow(2, n - 1, mo)]
    for i in range(1, n):
        d.append(d[-1] + ((a[i] - a[i - 1]) * pow(2, n - 1 - i, mo)))
    print(sum(d) % mo)




    return


if __name__ == "__main__":
    main()



C++ Code:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<iostream>
#include<math.h>
#include<map> 
#define endl '\n'
#define base 13331
#define mod 998244353
#define inf 0x3f3f3f3f3f3f3f3f
#define N 2000010
//#define endl '\n'
typedef long long ll;
using namespace std; 
ll a[N];
ll b[N];
ll kuaisumi(ll x,ll y){
	ll t,tt,tem;
	tem=y,t=1,tt=x;	
	while(tem){
		if(tem&1) t*=tt,t%=mod;	
		tt*=tt,tt%=mod;
		tem/=2;
	}
	return t;
}
ll c[N];
int main() {
	std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	ll i,n,ans;
	cin>>n;
	ans=0;
	for(i=1;i<=n;i++) cin>>a[i];
	for(i=1;i<=n;i++) b[i]=kuaisumi(2,n-i-1)*a[i]%mod;
	for(i=1;i<=n;i++) c[i]=c[i-1]+b[i],c[i]%=mod;
	for(i=1;i<=n;i++){
//		cout<<(c[i-1]+kuaisumi(2,n-i)*a[i]%mod)%mod<<" ";
		ans+=(c[i-1]+kuaisumi(2,n-i)*a[i]%mod)%mod;
		ans%=mod;
	}
	cout<<ans<<endl;
    return 0;
} 


Comments

Submit
0 Comments
More Questions

561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I